package demo

import (
	"fmt"
	"server/utils"
	"time"

	"github.com/gin-gonic/gin"
)

func WaitTime(ch chan string) {
	time.Sleep(time.Second * 3)
	param := <-ch
	// fmt.Println("异步执行")
	fmt.Printf("异步执行,参数:%#v\n", param)
	close(ch)
}

func GoRoutineHandle(c *gin.Context) {
	// 有缓存的channel，不阻塞后续程序执行
	ch := make(chan string, 1)
	ch <- "some data"
	go WaitTime(ch)
	fmt.Println("立即执行")

	// 正常返回数据
	c.JSON(
		200,
		utils.DealResponse(utils.Response{
			"data": "ok",
		}),
	)
}
